package myorg.myrpc.service;

import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.Slf4JLoggerFactory;

import com.google.common.util.concurrent.Service;

import myorg.myrpc.GlobalConfig;

import static myorg.myrpc.CommonConstant.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 启动 IMyrpc 服务
 * @author unknow_author
 *
 */
public class MyrpcServiceMain {	
	private static final MyrpcServiceConfig serviceConfig = MyrpcServiceConfig.getInstance();

	public MyrpcServiceMain() {
	}
	private static void waitquit(){
		System.out.println("PRESS 'quit' OR 'CTRL-C' to exit");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
		try{
			while(!"quit".equalsIgnoreCase(reader.readLine())){				
			}
			System.exit(0);
		} catch (IOException e) {

		}finally {

		}
	}
	public static void main(String[] args) {
		Logo.textLogo();
		serviceConfig.parseCommandLine(args);
		SyslogConfig.log4jConfig();
		// 设置slf4j记录日志,否则会有警告
		InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
		GlobalConfig.createDefaultUserConfigIfAbsent();
		if(GlobalConfig.getConfig().getBoolean(SERVER_START, true)){			
			@SuppressWarnings("unused")
			Service service = MyrpcService.buildService().startAsync();
		}
		if(GlobalConfig.getConfig().getBoolean(XHR_START, true)){
			@SuppressWarnings("unused")
			Service httpService = MyrpcService.buildHttpService().startAsync();
		}
		if(GlobalConfig.getConfig().getBoolean(RESTFUL_START, true)){
			MyrpcService.startRestfulService();
		}
		waitquit();
	}	
}
